<?php
/**
* Products Categories Rank Switch
*/
if (!defined("EXPONENT")) exit("");
$obj=null;

$obj->success=0;
if (exponent_permissions_check(array("administrate", "configure"),$loc) && isset($_REQUEST['stype'])) {
	$id=0;
	if (isset($_POST['id']))
		$id=intval($_POST['id']);
	$cat=$db->selectObject("productsmodule_category", "id = {$id}");
	if ($cat && ($cat->rank > 0))
	{
		// get the other item that we need to switch
		$sql_where = "AND rank < {$cat->rank}"; // move up
		if ($_REQUEST['stype'] == 1) // move down
		{
			$sql_where="AND rank > {$cat->rank}";
		}
		$sql_where.=" ORDER BY rank DESC";
		$temp=$db->selectObject("productsmodule_category", "location_data = '{$cat->location_data}'  AND parent_id = {$cat->parent_id} {$sql_where}");
		if ($temp)
		{
			$temp_value=$temp->rank;
			$temp->rank=$cat->rank;
			$cat->rank=$temp_value;
			$db->updateObject($temp,"productsmodule_category");
			$db->updateObject($cat,"productsmodule_category");
			//
			$obj->success=1;
			$obj->parent_id=$cat->parent_id; // parent id to reload :)
		}
	}
} else {
	//echo SITE_403_HTML;
}
echo json_encode($obj);
?>